﻿''' <summary>
''' This Module is createn by Robosturm and will be used to Test the Network of Commander Wars.
''' Espacially the Lobby.
''' </summary>
''' <remarks></remarks>
Module NetworkTest
    Public Sub NetworkTest(Optional ByVal Player As Integer = 1)
        If TypeOf Objektverwalter.Menue Is ObjektNetzwerkMenue Then
            Dim KMenue As ObjektNetzwerkMenue = Objektverwalter.Menue
            If KMenue.KAModus = ObjektKartenauswahlMenue.KAModi.SpielerAuswahl Then
                Konsole.Write("Start Network Test")
                Konsole.Write("Testing Player Data Transfer")
                'Testing CO Transfer
                Hauptmenue.NetzBefehlRecieved("Vorbereitung|KO|" + Player.ToString + "|ANDY|0")
                Hauptmenue.NetzBefehlRecieved("Vorbereitung|KO|" + Player.ToString + "|MAVERIK|1")
                If KMenue.KOS(Player, 0).KOID = "ANDY" And KMenue.KOS(Player, 1).KOID = "MAVERIK" Then
                    Hauptmenue.NetzBefehlRecieved("Vorbereitung|KO|" + Player.ToString + "|Nothing|0")
                    Hauptmenue.NetzBefehlRecieved("Vorbereitung|KO|" + Player.ToString + "|Nothing|1")
                    If KMenue.KOS(Player, 0) Is Nothing And KMenue.KOS(Player, 1) Is Nothing Then
                        Konsole.Write("KO Test Successful")
                    End If
                Else
                    Konsole.Write("KO Test Failed")
                    Konsole.Write("Exit Test")
                    Exit Sub
                End If
                'Testing AI Connection
                Hauptmenue.NetzBefehlRecieved("Vorbereitung|KI|" + Player.ToString + "|" + ObjektSpieler.KIStufen.Mensch.ToString)
                If KMenue.KIStufen(Player) = ObjektSpieler.KIStufen.Passiv Then
                    Hauptmenue.NetzBefehlRecieved("Vorbereitung|KI|" + Player.ToString + "|" + ObjektSpieler.KIStufen.Geschlossen.ToString)
                    If KMenue.KIStufen(Player) = ObjektSpieler.KIStufen.Geschlossen Then
                        Hauptmenue.NetzBefehlRecieved("Vorbereitung|KI|" + Player.ToString + "|" + ObjektSpieler.KIStufen.Einfach.ToString)
                        If KMenue.KIStufen(Player) = ObjektSpieler.KIStufen.Einfach Then
                            Hauptmenue.NetzBefehlRecieved("Vorbereitung|KI|" + Player.ToString + "|" + CInt(ObjektSpieler.KIStufen.Mensch).ToString)
                            If KMenue.KIStufen(Player) = ObjektSpieler.KIStufen.Passiv Then
                                Hauptmenue.NetzBefehlRecieved("Vorbereitung|KI|" + Player.ToString + "|" + CInt(ObjektSpieler.KIStufen.Geschlossen).ToString)
                                If KMenue.KIStufen(Player) = ObjektSpieler.KIStufen.Geschlossen Then
                                    Hauptmenue.NetzBefehlRecieved("Vorbereitung|KI|" + Player.ToString + "|" + CInt(ObjektSpieler.KIStufen.Einfach).ToString)
                                    If KMenue.KIStufen(Player) = ObjektSpieler.KIStufen.Einfach Then
                                        Konsole.Write("AI Test Successful")
                                    Else
                                        Konsole.Write("AI Test Failed")
                                        Konsole.Write("Exit Test")
                                        Exit Sub
                                    End If
                                Else
                                    Konsole.Write("AI Test Failed")
                                    Konsole.Write("Exit Test")
                                    Exit Sub
                                End If
                            Else
                                Konsole.Write("AI Test Failed")
                                Konsole.Write("Exit Test")
                                Exit Sub
                            End If
                        Else
                            Konsole.Write("AI Test Failed")
                            Konsole.Write("Exit Test")
                            Exit Sub
                        End If
                    Else
                        Konsole.Write("AI Test Failed")
                        Konsole.Write("Exit Test")
                        Exit Sub
                    End If
                Else
                    Konsole.Write("AI Test Failed")
                    Konsole.Write("Exit Test")
                    Exit Sub
                End If
                'Testing Team Transfer...
                Hauptmenue.NetzBefehlRecieved("Vorbereitung|TEAM|" + Player.ToString + "|C")
                If KMenue.Teams(Player) = "C" Then
                    Hauptmenue.NetzBefehlRecieved("Vorbereitung|TEAM|" + Player.ToString + "|A")
                    If KMenue.Teams(Player) = "A" Then
                        Konsole.Write("Team Test Successful")
                    Else
                        Konsole.Write("Team Test Failed")
                        Konsole.Write("Exit Test")
                        Exit Sub
                    End If
                Else
                    Konsole.Write("Team Test Failed")
                    Konsole.Write("Exit Test")
                    Exit Sub
                End If
                'Testing Color Transfer
                Hauptmenue.NetzBefehlRecieved("Vorbereitung|Farbe0|" + Player.ToString + "|" + Color.Blue.ToArgb.ToString)
                If KMenue.Farbtabellen(Player).ToArgb = Color.Blue.ToArgb Then
                    Hauptmenue.NetzBefehlRecieved("Vorbereitung|Farbe0|" + Player.ToString + "|" + Color.Red.ToArgb.ToString)
                    If KMenue.Farbtabellen(Player).ToArgb = Color.Red.ToArgb Then
                        Hauptmenue.NetzBefehlRecieved("Vorbereitung|Farbe1|" + Player.ToString + "|0")
                        If KMenue.Farbtabellen(Player).ToArgb = Hauptmenue.AlleFarbtabellen(0).ToArgb Then
                            Hauptmenue.NetzBefehlRecieved("Vorbereitung|Farbe1|" + Player.ToString + "|1")
                            If KMenue.Farbtabellen(Player).ToArgb = Hauptmenue.AlleFarbtabellen(1).ToArgb Then
                                Konsole.Write("Color Test Successful")
                            Else
                                Konsole.Write("Color Test Failed")
                                Konsole.Write("Exit Test")
                                Exit Sub
                            End If
                        Else
                            Konsole.Write("Color Test Failed")
                            Konsole.Write("Exit Test")
                            Exit Sub
                        End If
                    Else
                        Konsole.Write("Color Test Failed")
                        Konsole.Write("Exit Test")
                        Exit Sub
                    End If
                Else
                    Konsole.Write("Color Test Failed")
                    Konsole.Write("Exit Test")
                    Exit Sub
                End If
                'Testing Startfonds Transfer
                Hauptmenue.NetzBefehlRecieved("Vorbereitung|Startgeld|" + Player.ToString + "|1000")
                If KMenue.Startfonds(Player) = 1000 Then
                    Hauptmenue.NetzBefehlRecieved("Vorbereitung|Startgeld|" + Player.ToString + "|0")
                    If KMenue.Startfonds(Player) = 0 Then
                        Konsole.Write("Startfonds Test Successful")
                    Else
                        Konsole.Write("Startfonds Test Failed")
                        Konsole.Write("Exit Test")
                        Exit Sub
                    End If
                Else
                    Konsole.Write("Startfonds Test Failed")
                    Konsole.Write("Exit Test")
                    Exit Sub
                End If
                'Testing Fonds per Factory Transfer
                Hauptmenue.NetzBefehlRecieved("Vorbereitung|GeldFabrik|" + Player.ToString + "|1000")
                If KMenue.FondsproFabrik(Player) = 1000 Then
                    Hauptmenue.NetzBefehlRecieved("Vorbereitung|GeldFabrik|" + Player.ToString + "|0")
                    If KMenue.FondsproFabrik(Player) = 0 Then
                        Konsole.Write("Fonds per Factory Test Successful")
                    Else
                        Konsole.Write("Fonds per Factory Test Failed")
                        Konsole.Write("Exit Test")
                        Exit Sub
                    End If
                Else
                    Konsole.Write("Fonds per Factory Test Failed")
                    Konsole.Write("Exit Test")
                    Exit Sub
                End If
                'Testing Fonds per Town Transfer
                Hauptmenue.NetzBefehlRecieved("Vorbereitung|GeldStadt|" + Player.ToString + "|1000")
                If KMenue.FondsproStadt(Player) = 1000 Then
                    Hauptmenue.NetzBefehlRecieved("Vorbereitung|GeldStadt|" + Player.ToString + "|0")
                    If KMenue.FondsproStadt(Player) = 0 Then
                        Konsole.Write("Fonds per Town Test Successful")
                    Else
                        Konsole.Write("Fonds per Town Test Failed")
                        Konsole.Write("Exit Test")
                        Exit Sub
                    End If
                Else
                    Konsole.Write("Fonds per Town Test Failed")
                    Konsole.Write("Exit Test")
                    Exit Sub
                End If

                Hauptmenue.NetzBefehlRecieved("Vorbereitung|BaulisteFest|" + Player.ToString + "|AW")
                If KMenue.Baulisten(Player).Equals(New Bauliste(ObjektLadeklasse.GetBauliste(Commander_Wars.Baulisten.AW))) Then
                    Hauptmenue.NetzBefehlRecieved("Vorbereitung|BaulisteFest|" + Player.ToString + "|AW2")
                    If KMenue.Baulisten(Player).Equals(New Bauliste(ObjektLadeklasse.GetBauliste(Commander_Wars.Baulisten.AW_2))) Then
                        Konsole.Write("Construction List Fix Test Successful")
                    Else
                        Konsole.Write("Construction List Fix Test Failed")
                        Konsole.Write("Exit Test")
                        Exit Sub
                    End If
                Else
                    Konsole.Write("Construction List Fix Test Failed")
                    Konsole.Write("Exit Test")
                    Exit Sub
                End If
                'Next Test
                Hauptmenue.NetzBefehlRecieved("Vorbereitung|BaulisteOffen|" + Player.ToString + "|INF_MECH")
                If KMenue.Baulisten(Player).Equals(New Bauliste(New String() {"INF", "MECH"})) Then
                    Hauptmenue.NetzBefehlRecieved("Vorbereitung|BaulisteOffen|" + Player.ToString + "|INF_MECH_ART")
                    If KMenue.Baulisten(Player).Equals(New Bauliste(New String() {"INF", "MECH", "ART"})) Then
                        Konsole.Write("Construction List Custom Test Successful")
                    Else
                        Konsole.Write("Construction List Custom Test Failed")
                        Konsole.Write("Exit Test")
                        Exit Sub
                    End If
                Else
                    Konsole.Write("Construction List Custom Test Failed")
                    Konsole.Write("Exit Test")
                    Exit Sub
                End If
                Konsole.Write("Player Data Test Successful finished")
                'Well it seems like sending Data works
                'Next think is testing access...

                Konsole.Write("Starting Game Rule Test")
                Hauptmenue.NetzBefehlRecieved("Vorbereitung|Regeln|1|2|0_1|False|25_25_25_25|1|20|20|Nothing|False|2|False|1|False|False|False|True|False|True|100|True|30")
                If KMenue.Regeln.BauBlockade Then
                    If KMenue.Regeln.KeineGaben = True Then
                        If KMenue.Regeln.DoppelGabe = False Then
                            If KMenue.Regeln.GebaeudeZerstoerung = False Then
                                If KMenue.Regeln.KeineGebaeudeSchwachstelle = False Then
                                    If KMenue.Regeln.LevelModus = False Then
                                        If KMenue.Regeln.Luftschlacht = False Then
                                            If KMenue.Regeln.MaxSpieldauer = 20 Then
                                                If KMenue.Regeln.MaximaleanzahlderBesetzungen = 20 Then
                                                    If KMenue.Regeln.Nebel = Nebelarten.Nebel Then
                                                        If KMenue.Regeln.NoLuck = True Then
                                                            If KMenue.Regeln.Standardwetter = ObjektSpielfeld.Wetterarten.Regen Then
                                                                If KMenue.Regeln.StandartSiegbedingungen.Contains(ObjektRegeln.Siegbedingungen.KeineHQs) And KMenue.Regeln.StandartSiegbedingungen.Contains(ObjektRegeln.Siegbedingungen.KeineEinheiten) Then
                                                                    If KMenue.Regeln.Teurungsrate = 100 Then
                                                                        If KMenue.Regeln.Wetterwahrscheinlichkeiten(0) = 25 And KMenue.Regeln.Wetterwahrscheinlichkeiten(1) = 25 And KMenue.Regeln.Wetterwahrscheinlichkeiten(2) = 25 And KMenue.Regeln.Wetterwahrscheinlichkeiten(3) = 25 Then
                                                                            If KMenue.Regeln.Unbesiegbar Is Nothing Then
                                                                                If KMenue.Regeln.Zufallswetter = False Then
                                                                                    If KMenue.Regeln.MaxTroops = 30 Then
                                                                                        Konsole.Write("Game Rule Test Successful finished")
                                                                                        GoTo Successful
                                                                                    End If
                                                                                End If
                                                                            End If
                                                                        End If
                                                                    End If
                                                                End If
                                                            End If
                                                        End If
                                                    End If
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
                Konsole.Write("Game Rule Test Failed")
                Konsole.Write("Exit Test")
                Exit Sub
Successful:
                Konsole.Write("Network should work now!")

            End If
        End If
    End Sub
    ''' <summary>
    ''' Generates an IP out of a String IP Description
    ''' </summary>
    ''' <param name="IP"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetIP(ByVal IP As String) As System.Net.IPAddress
        Dim GIP As System.Net.IPAddress = Nothing
        If System.Net.IPAddress.TryParse(IP, GIP) Then
            If System.Net.IPAddress.TryParse(IP, GIP) Then
                Return GIP
            Else
                Try
                    Dim IPEnter As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(IP)
                    For I = 0 To IPEnter.AddressList.Length - 1
                        If IPEnter.AddressList(I).AddressFamily = Net.Sockets.AddressFamily.InterNetworkV6 Then
                            Return IPEnter.AddressList(I)
                        End If
                    Next
                Catch
                    Konsole.Write("Host IP not found.")
                End Try
            End If
        End If
        Return Nothing
    End Function
    Public Function GetIP() As System.Net.IPAddress
        Return GetMyAddress(Net.Sockets.AddressFamily.InterNetworkV6)
    End Function
    Public Function GetLocalIP() As System.Net.IPAddress
        Return GetMyAddress(Net.Sockets.AddressFamily.InterNetwork)
    End Function
    Public Function ConvertToNetworkIP(ByVal IP As System.Net.IPAddress) As System.Net.IPAddress
        Dim IPEnter As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(IP)
        For I = 0 To IPEnter.AddressList.Length - 1
            If IPEnter.AddressList(I).AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
                Return IPEnter.AddressList(I)
            End If
        Next
        Return IP
    End Function
    Public Function ConvertToInternetIP(ByVal IP As System.Net.IPAddress) As System.Net.IPAddress
        Dim IPEnter As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(IP)
        For I = 0 To IPEnter.AddressList.Length - 1
            If IPEnter.AddressList(I).AddressFamily = Net.Sockets.AddressFamily.InterNetworkV6 Then
                Return IPEnter.AddressList(I)
            End If
        Next
        Return IP
    End Function
    Private Function GetMyAddress(ByVal addressFamily As Net.Sockets.AddressFamily) As System.Net.IPAddress
        Dim IPEnter As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
        'System.Net.Dns.GetHostName
        Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("")
        For Each address In h.AddressList
            If address.AddressFamily = addressFamily Then
                Return address
            End If
        Next
        ' Dim IPEnter As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
        For I = 0 To IPEnter.AddressList.Length - 1
            If IPEnter.AddressList(I).IsIPv6LinkLocal = False Then
                If IPEnter.AddressList(I).IsIPv6Multicast = False Then
                    If IPEnter.AddressList(I).IsIPv6SiteLocal = False Then
                        Return IPEnter.AddressList(I)
                    End If
                End If
            End If
        Next
        Return New System.Net.IPAddress(0)
    End Function
    Private Function GetAllMyAddress() As System.Net.IPAddress()
        'System.Net.Dns.GetHostName
        Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("")
        Return h.AddressList.ToArray()
    End Function
End Module
